package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 预约设置服务
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    //批量添加
    public void add(List<OrderSetting> list) {
        if (list != null && list.size() > 0) {
            for (OrderSetting orderSetting : list) {
                editNumberByDate(orderSetting);
            }
        }
    }

    @Override
    public List<Map<String, Integer>> getOrderSettingByMonth(String data) {
        String year = data.substring(0, data.lastIndexOf("-"));
        String month = data.substring(data.lastIndexOf("-") + 1);
        Map<String, String> map = new HashMap<>();
        map.put("year", year);
        map.put("month", month);
        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(map);
        List<Map<String, Integer>> result = new ArrayList<>();
        for (OrderSetting orderSetting : list) {
            Map<String, Integer> map1 = new HashMap<>();
            map1.put("date", orderSetting.getOrderDate().getDate());
            map1.put("number", orderSetting.getNumber());
            map1.put("reservations", orderSetting.getReservations());
            result.add(map1);
        }
        return result;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        //检查此数据（日期）是否存在
        long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if (count > 0) {
            //已经存在，执行更新操作
            orderSettingDao.editNumberByOrderDate(orderSetting);
        } else {
            //不存在，执行添加操作
            orderSettingDao.add(orderSetting);
        }
    }
}